/* eslint-disable array-callback-return */
import React from 'react';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Card, Button, Spin, Select, message, Steps } from 'antd';
import { connect } from 'dva';
import router from 'umi/router';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import SearchEmp from '../../components/SearchEmp';
import HOCSelect from '@/components/AntDesign/Select';

const FormItem = Form.Item;
const { Option } = Select;
const { Step } = Steps;

@connect(({ adjusttask, loading, publicdata: { empInfo } }) => ({
  loading: loading.models.adjusttask || false,
  empNameList: adjusttask.empNameList,
  adjustInfo: adjusttask.adjustInfo,
  adjusttask,
  empInfo,
  isClear: adjusttask.isClear,
}))
@Form.create()
class BasicForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      sendAdjType: [], // 调整项字典
    };
  }

  componentDidMount() {
    const { dispatch } = this.props;
    // 获取调整项的字典值
    dispatch({
      type: 'publicdata/fetchDictCode',
      payload: ['SEND_ADJ_TYPE'],
    }).then(data => {
      let sendAdjType = [];
      if (data && data.SEND_ADJ_TYPE && data.SEND_ADJ_TYPE.length) {
        sendAdjType = data.SEND_ADJ_TYPE.filter(
          item => item.itemCode !== '5' && item.itemCode !== '6' && item.itemCode !== '4'
        );
      }
      this.setState({ sendAdjType });
    });
  }

  // 提价数据
  submit = () => {
    const { form, dispatch, adjustInfo, empInfo } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const { adjType } = values;
        let modelNameSpace = 'adjusttask';
        modelNameSpace =
          // eslint-disable-next-line no-nested-ternary
          adjType === '1'
            ? 'adjusttask'
            : // eslint-disable-next-line no-nested-ternary
            adjType === '2'
            ? 'adjustaccu'
            : adjType === '3'
            ? 'adjustinfo'
            : 'adjustarea';

        const routerPush = () => {
          dispatch({
            type: 'adjusttask/updateModelData',
            payload: {
              isClear: false,
              adjType,
            },
          });
          dispatch({
            type: `${modelNameSpace}/updateModelData`,
            payload: {
              adjustInfo: { ...adjustInfo, ...empInfo },
            },
          });
          switch (adjType) {
            case '1':
              router.push(
                `/customservicemanage/adjusttask/adjustins?custId=${empInfo.custId}&sendId=${
                  empInfo.sendId
                }`
              );
              break;
            case '2':
              router.push(
                `/customservicemanage/adjusttask/adjustaccu?custId=${empInfo.custId}&sendId=${
                  empInfo.sendId
                }`
              );
              break;
            case '3':
              router.push(
                `/customservicemanage/adjusttask/adjustinfo?custId=${empInfo.custId}&sendId=${
                  empInfo.sendId
                }`
              );
              break;
            case '4':
              // router.push(
              //   `/customservicemanage/adjusttask/adjustarea?custId=${values.custId}&sendId=${
              //     values.sendId
              //   }&idCode=${values.idCode}`
              // );
              message.warning('努力开发中。。。');
              break;
            default:
              break;
          }
        };

        if (adjType !== '4') {
          dispatch({
            type: `${modelNameSpace}/fetchNextStep`,
            payload: { sendId: empInfo.sendId },
          }).then(response => {
            if (response.success) {
              routerPush();
            } else {
              message.warning(response.message || '获取人员信息：服务端错误');
            }
          });
        } else {
          routerPush();
        }
      }
    });
  };

  render() {
    const { loading, form, empInfo, isClear, adjusttask } = this.props;
    const { getFieldDecorator } = form;
    const { sendAdjType } = this.state;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
        md: { span: 10 },
      },
    };
    return (
      <Form>
        <PageHeaderWrapper>
          <Spin tip="Loading..." spinning={loading}>
            <Card>
              <Steps current={0} style={{ marginBottom: 70 }}>
                <Step title="选择调整项" />
                <Step title="填写修改信息" />
                <Step title="完成" />
              </Steps>
              <SearchEmp
                params={{ taskSource: 1 }}
                form={form}
                optionKey="sendId"
                isClear={isClear}
              />
              <FormItem {...formItemLayout} label="调整项">
                {getFieldDecorator('adjType', {
                  rules: [
                    {
                      required: true,
                      message: '请选择',
                    },
                  ],
                  initialValue: isClear ? undefined : adjusttask.adjType,
                })(
                  <HOCSelect placeholder="请选择" showSearch optionFilterProp="children">
                    {sendAdjType &&
                      sendAdjType.map(item => (
                        <Option key={item.itemCode} value={item.itemCode}>
                          {item.itemName}
                        </Option>
                      ))}
                  </HOCSelect>
                )}
              </FormItem>
              <div style={{ marginTop: '100px', marginBottom: '30px', textAlign: 'center' }}>
                <Button style={{ marginRight: '30px' }} onClick={() => router.goBack()}>
                  返回
                </Button>
                <Button
                  type="primary"
                  onClick={this.submit}
                  disabled={!(empInfo && empInfo.sendId)}
                >
                  下一步
                </Button>
              </div>
            </Card>
          </Spin>
        </PageHeaderWrapper>
      </Form>
    );
  }
}

const BasicFormList = Form.create()(BasicForm);
export default BasicFormList;
